package com.lzxuni.modules.index;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.FileEntity;
import com.lzxuni.modules.common.service.FileEntityService;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import com.lzxuni.modules.enterprise.entity.EnterpriseInfo;
import com.lzxuni.modules.enterprise.service.EnterpriseInfoService;
import com.lzxuni.modules.informationpublish.entity.InformationPublishPressRelease;
import com.lzxuni.modules.informationpublish.service.InformationPublishPressReleaseService;
import com.lzxuni.modules.productdisplay.entity.ProductDisplay;
import com.lzxuni.modules.productdisplay.service.ProductDisplayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedList;
import java.util.List;

/**
 * IndexController
 *
 * @author liuzp
 * @version 1.0
 * @createTime 2019-06-27 22:42
 * @description 
 **/
@RestController
public class IndexController extends BaseController {

    @Autowired
    private ChainService chainService;
    @Autowired
    private InformationPublishPressReleaseService informationPublishPressReleaseService;
    @Autowired
    private FileEntityService fileEntityService;
    @Autowired
    private EnterpriseInfoService enterpriseInfoService;
    @Autowired
    private ProductDisplayService productDisplayService;

    @RequestMapping("/")
    public ModelAndView index() throws Exception {
        ModelAndView mv = new ModelAndView("/index");
        //新闻中心
        List<InformationPublishPressRelease> ipprList = informationPublishPressReleaseService.list(new QueryWrapper<InformationPublishPressRelease>().ne("news_type","bgt").orderByDesc("create_time").last("limit 4"));
       for(int i=0;i<ipprList.size();i++){
           ipprList.get(i).setContent(StringUtils.removeHtml(ipprList.get(i).getContent()));
           FileEntity fileEntity = new FileEntity();
           String id = ipprList.get(i).getNewsId();
           fileEntity.setYwId(id);
           fileEntity.setYwType("information-news");
           List<FileEntity> list1 = fileEntityService.queryListByFileEntity(fileEntity);
           if(list1.size()>0){
               LinkedList<FileEntity> fileEntities1 = new LinkedList<>();
               fileEntities1.add((FileEntity)list1.get(0));
               ipprList.get(i).setFileEntities(fileEntities1);
           }else{
               ipprList.get(i).setFileEntities(list1);
           }
       }
        mv.addObject("ipprList", ipprList);
        //新增企业
        List<EnterpriseInfo> enterpriseList = enterpriseInfoService.list(new QueryWrapper<EnterpriseInfo>().eq("approval_result","通过").orderByDesc("create_time").last("limit 4"));
        List list2 = null;
        for (int i = 0;i < enterpriseList.size();i ++){
            FileEntity fileEntity = new FileEntity();
            String id = enterpriseList.get(i).getYwId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("demopic");
            list2 = fileEntityService.queryListByFileEntity(fileEntity);
            if(list2.size()>0){
                LinkedList<FileEntity> fileEntities2 = new LinkedList<>();
                fileEntities2.add((FileEntity)list2.get(0));
                enterpriseList.get(i).setFileEntities(fileEntities2);
            }else{
                enterpriseList.get(i).setFileEntities(list2);
            }
        }
        mv.addObject("enterpriseList",enterpriseList);
        //曝光台
        List<InformationPublishPressRelease> bgtList = informationPublishPressReleaseService.list(new QueryWrapper<InformationPublishPressRelease>().eq("news_type","bgt").orderByDesc("create_time").last("limit 6"));
        List list4 = null;
        for (int i = 0;i < bgtList.size();i ++){
            bgtList.get(i).setContent(StringUtils.removeHtml(bgtList.get(i).getContent()));
            FileEntity fileEntity = new FileEntity();
            String id = bgtList.get(i).getNewsId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("information-bgt");
            list4 = fileEntityService.queryListByFileEntity(fileEntity);
            if(list4.size()>0){
                LinkedList<FileEntity> fileEntities4 = new LinkedList<>();
                fileEntities4.add((FileEntity)list4.get(0));
                bgtList.get(i).setFileEntities(fileEntities4);
            }else{
                bgtList.get(i).setFileEntities(list4);
            }
        }
        mv.addObject("bgtList", bgtList);
        //产品展示
        List<ProductDisplay> productlist = productDisplayService.list(new QueryWrapper<ProductDisplay>().orderByDesc("create_time").last("limit 8"));
        List list3 =null;
        for (int i= 0;i<productlist.size();i++){
            productlist.get(i).setProductFy(StringUtils.removeHtml(productlist.get(i).getProductFy()));
            FileEntity fileEntity = new FileEntity();
            String id = productlist.get(i).getYwId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("demopic");
            list3 = fileEntityService.queryListByFileEntity(fileEntity);
            if(list3.size()>0){
                LinkedList<FileEntity> fileEntities3 = new LinkedList<>();
                fileEntities3.add((FileEntity)list3.get(0));
                productlist.get(i).setFileEntities(fileEntities3);
            }else{
                productlist.get(i).setFileEntities(list3);
            }
        }
        mv.addObject("productlist",productlist);
        return mv;
    }

    //网址站首页
    @RequestMapping("/index.html")
    public ModelAndView indexhtml() throws Exception {
        ModelAndView mv = new ModelAndView("/index");
        //新闻中心
        List<InformationPublishPressRelease> ipprList = informationPublishPressReleaseService.list(new QueryWrapper<InformationPublishPressRelease>().ne("news_type","bgt").orderByDesc("create_time").last("limit 4"));
        List list1 = null;
        for(int i=0;i<ipprList.size();i++){
            ipprList.get(i).setContent(StringUtils.removeHtml(ipprList.get(i).getContent()));
            FileEntity fileEntity = new FileEntity();
            String id = ipprList.get(i).getNewsId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("information-news");
            list1 = fileEntityService.queryListByFileEntity(fileEntity);
            if (list1.size()>0){
                LinkedList<FileEntity> fileEntities1 = new LinkedList<>();
                fileEntities1.add((FileEntity) list1.get(0));
                ipprList.get(i).setFileEntities(fileEntities1);
            }else{
                ipprList.get(i).setFileEntities(list1);
            }
        }
        mv.addObject("ipprList", ipprList);
        //新增企业
        List<EnterpriseInfo> enterpriseList = enterpriseInfoService.list(new QueryWrapper<EnterpriseInfo>().eq("approval_result","通过").orderByDesc("create_time").last("limit 4"));
        List list2 = null;
        for (int i = 0;i < enterpriseList.size();i ++){
            FileEntity fileEntity = new FileEntity();
            String id = enterpriseList.get(i).getYwId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("demopic");
            list2 = fileEntityService.queryListByFileEntity(fileEntity);
            if(list2.size()>0){
                LinkedList<FileEntity> fileEntities2 = new LinkedList<>();
                fileEntities2.add((FileEntity)list2.get(0));
                enterpriseList.get(i).setFileEntities(fileEntities2);
            }else{
                enterpriseList.get(i).setFileEntities(list2);
            }
            enterpriseList.get(i).setFileEntities(list2);
        }
        mv.addObject("enterpriseList",enterpriseList);
        //曝光台
        List<InformationPublishPressRelease> bgtList = informationPublishPressReleaseService.list(new QueryWrapper<InformationPublishPressRelease>().eq("news_type","bgt").orderByDesc("create_time").last("limit 6"));
        List list4 = null;
        for (int i = 0;i < bgtList.size();i ++){
            bgtList.get(i).setContent(StringUtils.removeHtml(bgtList.get(i).getContent()));
            FileEntity fileEntity = new FileEntity();
            String id = bgtList.get(i).getNewsId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("information-bgt");
            list4 = fileEntityService.queryListByFileEntity(fileEntity);
            if(list4.size()>0){
                LinkedList<FileEntity> fileEntities4 = new LinkedList<>();
                fileEntities4.add((FileEntity)list4.get(0));
                bgtList.get(i).setFileEntities(fileEntities4);
            }else{
                bgtList.get(i).setFileEntities(list4);
            }
        }
        mv.addObject("bgtList", bgtList);
        //产品展示
        List<ProductDisplay> productlist = productDisplayService.list(new QueryWrapper<ProductDisplay>().orderByDesc("create_time").last("limit 8"));
        List list3 =null;
        for (int i= 0;i<productlist.size();i++){
            productlist.get(i).setProductFy(StringUtils.removeHtml(productlist.get(i).getProductFy()));
            FileEntity fileEntity = new FileEntity();
            String id = productlist.get(i).getYwId();
            fileEntity.setYwId(id);
            fileEntity.setYwType("demopic");
            list3 = fileEntityService.queryListByFileEntity(fileEntity);
            if(list3.size()>0){
                LinkedList<FileEntity> fileEntities3 = new LinkedList<>();
                fileEntities3.add((FileEntity)list3.get(0));
                productlist.get(i).setFileEntities(fileEntities3);
            }else{
                productlist.get(i).setFileEntities(list3);
            }
            productlist.get(i).setFileEntities(list3);
        }
        mv.addObject("productlist",productlist);
        return mv;
    }
//    @RequestMapping("/index")
//    public ModelAndView index2(){
//        ModelAndView mv = new ModelAndView("/index");
//        return mv;
//    }
    @RequestMapping(value="/news/get/{id}",method= RequestMethod.GET)
    public Chain get2(@PathVariable("id") String id)
    {
        Chain chain = chainService.getById(id);
        return chain;
    }
}
